import time

from code_builder.tools import utils, code_builder


class create_sql(object):
    def __init__(self, data):
        self.data = data

    def create_sql(self):
        result = ""
        for table_name in self.data:
            # 遍历表格
            seq_name = table_name + "_seq_id"
            trigger_name = table_name + "_ins_id"
            note_sql = self.create_note(table_name)
            table_sql = self.create_table_sql(table_name, self.data[table_name])
            comment_sql = self.create_comment_sql(table_name, self.data[table_name])
            seq_sql = self.create_seq_sql(seq_name)
            trigger_sql = self.create_trigger_sql(table_name, trigger_name, seq_name)
            sql = code_builder.sql_create_one_to_all(note_sql=note_sql, table_sql=table_sql, comment_sql=comment_sql,
                                                     seq_sql=seq_sql, trigger_sql=trigger_sql)
            result += sql
        return self.fmt(result)

    def fmt(self, sql):
        # TODO 将sql语句格式化为可读形式
        return sql

    def create_note(self, table_name):
        return code_builder.sql_create_note(table_name)

    def create_table_sql(self, table_name, data):
        content = ""
        for i in range(len(data)):
            content += self.create_content(data[i])
            if i != len(data) - 1:
                content += ','
        return code_builder.sql_create_table(table_name, content)

    def create_content(self, row):
        field_code = row['field_code']
        field_type = row['field_type']
        if row['field_len'] == "":
            field_len = ""
        else:
            field_len = str(int(row['field_len']))
        if row['field_dec'] == "":
            field_dec = ""
        else:
            field_dec = str(int(row['field_dec']))
        field_restrain = ""
        table_primary = ""
        if field_type == 'NUMBER' or field_type == 'FLOAT':
            if row["field_dec"] != "":
                field_type = "NUMBER({field_len},{field_dec})".format(field_len=field_len, field_dec=field_dec)
            else:
                field_type = "NUMBER"
        elif field_type == 'VARCHAR2' or field_type == 'CHAR' or field_type == 'NVARCHAR2':
            if row['field_len'] != "":
                field_type = "{field_type}({field_len})".format(field_type=field_type, field_len=field_len)
            else:
                field_type = "{field_type}(50)".format(field_type=field_type)
        if row["field_restrain"] == 'M':
            field_restrain = "NOT NULL"
        if row["field_name"] == "主键":
            table_primary = "PRIMARY KEY"
        return code_builder.sql_create_table_col(field_code, field_type, field_restrain, table_primary)

    def create_comment_sql(self, table_name, data):
        result = ""
        for ob in data:
            field_code = ob['field_code'].replace(" ", "")
            field_name = ob['field_name']
            result += code_builder.sql_create_comment(table_name, field_code, field_name)
        return result

    def create_seq_sql(self, seq_name):
        return code_builder.sql_create_seq(seq_name)

    def create_trigger_sql(self, table_name, trigger_name, seq_name):
        return code_builder.sql_create_trigger(table_name, trigger_name, seq_name)


def create_sql_file(data, target_path):
    sql = create_sql(data).create_sql()
    now_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
    sql_file_name = "sql_{now_time}.sql".format(now_time=now_time)
    create_sql_file_isSuccess = utils.create_file(target_path, sql_file_name, sql)
    if create_sql_file_isSuccess:
        utils.print_create_success("sql")
